package o.gwtp.editor.client.editor;

import o.gwtp.editor.client.editor.EditorPresenter.EditorProxy;

import com.google.inject.Inject;
import com.google.inject.Provider;
import com.gwtplatform.mvp.client.EventBus;
import com.gwtplatform.mvp.client.proxy.PlaceManager;
import com.gwtplatform.mvp.client.proxy.ProxyFailureHandler;

/**
 * @author Olivier Monaco
 */
public class EditorProxyFactory {
  private Provider<EditorPresenterProvider> provider;
  private ProxyFailureHandler failureHandler;
  private PlaceManager placeManager;
  private EventBus eventBus;
  private int count = 0;

  @Inject
  public EditorProxyFactory(Provider<EditorPresenterProvider> provider,
      ProxyFailureHandler failureHandler, PlaceManager placeManager,
      EventBus eventBus) {
    this.provider = provider;
    this.failureHandler = failureHandler;
    this.placeManager = placeManager;
    this.eventBus = eventBus;
  }

  public EditorProxy create(String name) {
    EditorPresenterProvider presenter = provider.get();
    presenter.setName(name);
    EditorProxyImpl proxy = new EditorProxyImpl(presenter, "editor" + count++);
    proxy.bind(failureHandler, placeManager, eventBus);
    return proxy;
  }
}
